package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxit.tdf.server.entity.Course;
import com.sxit.tdf.server.entity.bo.CourseInfoBo;
import com.sxit.tdf.server.entity.bo.SimpleCourseInfoBo;
import com.sxit.tdf.server.entity.vo.DeleteCourseInfoVo;
import com.sxit.tdf.server.entity.vo.InsertCourseInfoVo;
import com.sxit.tdf.server.entity.vo.SiftCourseInfoVo;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.ICourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * 前端控制器
 *
 * @author 橙子高
 * @since 2022-02-18
 */
@RestController
@RequestMapping("/course")
@Tag(name = "CourseController", description = "课程相关接口")
public class CourseController {

  @Autowired private ICourseService courseService;

  @Operation(summary = "获取课程列表")
  @PostMapping("/getCourseList")
  @SaCheckLogin
  @SaCheckPermission("COURSE:SELECT")
  public R getCourseList(@Valid @RequestBody SiftCourseInfoVo siftCourseInfoVo) {
    IPage<CourseInfoBo> courseInfoBo = courseService.getCourseInfoBo(siftCourseInfoVo);

    return R.ok().put("data", courseInfoBo.getRecords()).put("total", courseInfoBo.getTotal());
  }

  @Operation(summary = "新增课程列表")
  @PostMapping("/insertOrUpdateCourseInfo")
  @SaCheckLogin
  @SaCheckPermission(
      value = {"COURSE:UPDATE", "COURSE:INSERT"},
      mode = SaMode.OR)
  public R insertCourseInfo(@Valid @RequestBody InsertCourseInfoVo insertCourseInfoVo) {

    if (insertCourseInfoVo.getId() == null) {
      Course course = new Course();
      course.setDeptId(insertCourseInfoVo.getDeptName());
      course.setCourseName(insertCourseInfoVo.getCourseName());
      course.setCourseCode(insertCourseInfoVo.getCourseCode());
      course.setProfessionId(insertCourseInfoVo.getProfessionalName());
      course.setTeachId(insertCourseInfoVo.getTeacher());
      course.setClassesId(insertCourseInfoVo.getClasses());
      course.setCourseStatu(insertCourseInfoVo.getClassStatu());
      course.setCourseGrade(insertCourseInfoVo.getCourseGrade());
      boolean save = courseService.save(course);

      if (save) {
        return R.ok("保存成功");
      } else {
        return R.error("保存失败");
      }
    } else {
      Course course = new Course();
      course.setId(insertCourseInfoVo.getId());
      course.setDeptId(insertCourseInfoVo.getDeptName());
      course.setCourseName(insertCourseInfoVo.getCourseName());
      course.setCourseCode(insertCourseInfoVo.getCourseCode());
      course.setProfessionId(insertCourseInfoVo.getProfessionalName());
      course.setTeachId(insertCourseInfoVo.getTeacher());
      course.setClassesId(insertCourseInfoVo.getClasses());
      course.setCourseStatu(insertCourseInfoVo.getClassStatu());
      course.setCourseGrade(insertCourseInfoVo.getCourseGrade());
      boolean save = courseService.updateById(course);

      if (save) {
        return R.ok("修改成功");
      } else {
        return R.error("修改失败");
      }
    }
  }

  @Operation(summary = "通过Id删除")
  @PostMapping("/delete")
  @SaCheckLogin
  @SaCheckPermission("COURSE:DELETE")
  public R delete(@Valid @RequestBody DeleteCourseInfoVo deleteCourseInfoVo) {

    boolean delete = courseService.removeById(deleteCourseInfoVo.getId());
    if (delete) {
      return R.ok("删除成功");
    } else {
      return R.error("删除失败");
    }
  }

  @Operation(summary = "通过Id获取课程信息")
  @PostMapping("/getCourseInfoById")
  @SaCheckLogin
  public R getCourseInfoById(@Valid @RequestBody DeleteCourseInfoVo deleteCourseInfoVo) {
    Course byId = courseService.getById(deleteCourseInfoVo.getId());
    return R.ok().put("data", byId);
  }

  @Operation(summary = "获取简易的课程信息")
  @GetMapping("/getCourseList")
  @SaCheckLogin
  public R getCourseList(@PathParam("classId") Integer classId) {

    List<SimpleCourseInfoBo> list = courseService.getSimpleCourseInfoBoList(classId);
    return R.ok().put("data", list);
  }
}
